#include "GpioTest.h"
#include <QDebug>

GpioTest::GpioTest(QObject *parent)
    : QObject{parent}
{
	process = new QProcess(this);  // 创建 QProcess 对象

    // 连接信号和槽
    connect(process, SIGNAL(readyReadStandardOutput()), this, SLOT(onProcessReadyReadStandardOutput()));
    connect(process, SIGNAL(readyReadStandardError()), this, SLOT(onProcessReadyReadStandardError()));
    connect(process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(onProcessFinished(int, QProcess::ExitStatus)));
}


void GpioTest::sendSshCommand(const QString &host, const QString &user, const QString &command)
{
    qDebug() << "Sending SSH command...";

    // 确保进程处于非运行状态
    if (process->state() == QProcess::NotRunning) {
        // 执行远程命令
        executeSshCommand(host, user, command);
    } else {
        qDebug() << "Process is already running.";
        emit commandError("Process is already running.");
    }
}

void GpioTest::executeSshCommand(const QString &host, const QString &user, const QString &command)
{
    qDebug() << "Starting SSH command...";

    // 构造 SSH 命令并启动
    QString sshCommand = QString("ssh %1@%2 %3").arg(user).arg(host).arg(command);
    process->start(sshCommand);
}

void GpioTest::sendRemoteCommand(const QString &host, const QString &user, const QString &commandPath)
{
    qDebug() << "Sending SSH command to execute remote command...";

    // 确保进程处于非运行状态
    if (process->state() == QProcess::NotRunning) {
        QString command = commandPath;   // 将路径作为命令

        // 执行远程命令
        executeSshCommand(host, user, command);
    } else {
        qDebug() << "Process is already running.";
        emit commandError("Process is already running.");
    }
}

void GpioTest::executeRemoteSshCommand(const QString &host, const QString &user, const QString &command)
{
    qDebug() << "Starting SSH command...";

    // 启动 SSH 进程并执行命令
    process->start("ssh", QStringList() << (user + "@" + host) << command);
}


void GpioTest::onProcessReadyReadStandardOutput()
{
    qDebug() << "onProcessReadyReadStandardOutput:";
    QByteArray output = process->readAllStandardOutput();
    if (!output.isEmpty()) {
        // 将输出转换为 UTF-8 编码的 QString
        QString outputStr = QString::fromUtf8(output);
        _resultOutput = outputStr;
        qDebug() << "Command Output:" << outputStr;
        emit commandOutput(outputStr);  // 触发输出信号
        emit resultOutputChanged(_resultOutput);
    }
}

void GpioTest::onProcessReadyReadStandardError()
{
     qDebug() << "onProcessReadyReadStandardError:";
    QByteArray error = process->readAllStandardError();
    if (!error.isEmpty()) {
        qDebug() << "Command Error:" << error;
        emit commandError(QString(error));  // 错误输出
    }
}

void GpioTest::onProcessFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
    if (exitStatus == QProcess::NormalExit && exitCode == 0) {
        qDebug() << "Process finished successfully.";
    } else {
        qDebug() << "Process finished with errors. Exit Code:" << exitCode;
    }

    // 进程结束后，允许再次执行命令
    emit processFinished();  // 发出 processFinished 信号

    qDebug() << "Process state after finishing:" << process->state();
}
